package com.flong.common;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *@Author:jilongliang
 *@Email:jilongliang@sina.com
 *@Date:2014-2-23
 *@Description:
 */
@SuppressWarnings("all")
public class HttpRequestUtils {

    /**
     * 发http请求
     * POST和GET请求都可
     * @param requestUrl 请求地址
     * @param method传入的执行的方式 是GET或POST方式
     * @return String
     */
    public static String HttpURLConnRequest(String requestUrl, String param, String method) {
        StringBuffer buffer = new StringBuffer();
        try {
            URL url = new URL(requestUrl + "?tid=" + param);
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setDoInput(true);
            httpUrlConn.setRequestMethod(method);
            httpUrlConn.setUseCaches(false);  
            httpUrlConn.setInstanceFollowRedirects(true); //重定
            httpUrlConn.setConnectTimeout(1000000);
            httpUrlConn.connect();
            // 将返回的输入流转换成字符
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }
    
	/**
	 * 发http请求
	 * POST和GET请求都可
	 * @param requestUrl 请求地址
	 * @param method传入的执行的方式 是GET或POST方式
	 * @return String
	 */
	public static String HttpURLConnRequest(String requestUrl,String method) {
		StringBuffer buffer = new StringBuffer();
		try {
			URL url = new URL(requestUrl);
			HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
			httpUrlConn.setDoInput(true);
			httpUrlConn.setRequestMethod(method);
			httpUrlConn.setUseCaches(false);  
			httpUrlConn.setInstanceFollowRedirects(true); //重定
			httpUrlConn.setConnectTimeout(1000000);
			httpUrlConn.connect();
			// 将返回的输入流转换成字符
			InputStream inputStream = httpUrlConn.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			bufferedReader.close();
			inputStreamReader.close();
			// 释放资源
			inputStream.close();
			inputStream = null;
			httpUrlConn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}
	  
	/**
	 * 获取文件
	 * @param buffer
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	private static String getStreamReader(StringBuffer buffer, InputStream inputStream)throws IOException {
		String content;
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		String str = null;
		while ((str = bufferedReader.readLine()) != null) {
			buffer.append(str);
			buffer.append("\n");
		}
		content=buffer.toString();
		// 释放资源
		bufferedReader.close();
		inputStreamReader.close();
		inputStream.close();
		inputStream = null;
		return content;
	}
}
